<?php


/**
 * Static methods to manage the bot
 * 
 * @static 
 * @author David Monllaó <david.monllao@gmail.com>
 */
class System {

	/**
	 * Pending blogs to parse
	 * @static
	 * @var
	 */
    public static $blogs = false;

    /**
     * Available locales on the system
     * @static
     * @var array
     */
    public static $LOCALES;

    /**
     * Available aplication text strings
     * @var array
     */
    public static $S;
    
    
    /**
     * Gets the next blogs list
     * @static
     * @param integer $key
     */
    public static function loadPendingBlogs($key = 0) {

        $result = getPendingBlogs();
        if ($result) {
            for ($i = 0; $i < count($result); $i++) {
                System::$blogs[$key] = array('id' => $result[$i]->id,
                    'localeid' => NULL,
                    'url' => $result[$i]->url,
                    'name' => $result[$i]->name);
                $key++;
            }
        }
    }


    /**
     * Stores the DB locales into a global scope
     * @static
     */
    public static function loadSystemLocales() {

        $results = getSystemLocales();
        if (!$results) {
            return false;
        }

        // Name as the key to check with !empty()
        foreach ($results as $result) {
            System::$LOCALES[$result->name] = $result->id;
        }
    }


    /**
     * Gets the locale id
     * @static
     * @param sting $locale
     * @return integer
     */
    public static function getLocaleId($locale) {
        
        if (!System::$LOCALES) {
            System::loadSystemLocales();
        }

        if (!empty(System::$LOCALES) && !empty(System::$LOCALES[$locale])) {
            return System::$LOCALES[$locale];
        }

        $localeobj = new LocaleLang($locale);
        if ($localeid = $localeobj->insert()) {
            System::loadSystemLocales();
            return $localeid;
        }
        
        // If no lang has been detected we label... something
        return 1;
    }

    
    /**
     * Initializes System::$S with the selected language texts
     */
    public static function setLanguage() {
	    	
	    global $CONFIG;
	
	    if (!empty($_GET["hl"])) {
	       $_SESSION["lang"] = substr($_GET["hl"], 0, 2);
	    }
	
	    if (empty($_SESSION['lang']) && !empty($_SERVER["HTTP_ACCEPT_LANGUAGE"])) {
	        $_SESSION['lang'] = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0, 2);
	    }
	    
	    // If it's not translated default lant
	    if (!file_exists($CONFIG->datadir.'/lang/'.$_SESSION["lang"].'.php')) {
	        $_SESSION["lang"] = $CONFIG->defaultlanguage;
	    }
	
	    include_once($CONFIG->datadir.'/lang/'.$_SESSION["lang"].'.php');
	    
	    System::$S = $s;
    }
    
}
